package com.orangelife.mobile.individual.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.curry.android.adapter.FatherAdapter;
import com.curry.android.http.HttpUtil;
import com.curry.android.util.StringUtil;
import com.curry.orangelife.mobile.R;
import com.orangelife.mobile.common.activity.OrangeLifeBaseActivity;
import com.orangelife.mobile.common.biz.JSONRequest;
import com.orangelife.mobile.constants.Constant;
import com.orangelife.mobile.util.JsonReponseHandler;
import com.orangelife.mobile.util.ToastHelper;
import com.orangelife.mobile.widget.EditText.ClearEditText;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * 快递公司模糊查询
 */
public class ExpressCompanyActivity extends OrangeLifeBaseActivity implements View.OnClickListener {

    @Bind(R.id.etSearch)
    ClearEditText etSearch;
    @Bind(R.id.iv_title_right)
    ImageView ivTitleRight;
    @Bind(R.id.tvCancel)
    TextView tvCancel;
    @Bind(R.id.lvCompany)
    ListView lvCompany;
    private List<Map<String, Object>> mapList;
    private MyAdapter adapter;
    private String company="";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_express_company);
        ButterKnife.bind(this);
        initView();
        searchCompany();

    }

    private void initView() {
        ivTitleRight.setOnClickListener(this);
        tvCancel.setOnClickListener(this);
        mapList = new ArrayList<>();
        adapter = new MyAdapter();
        lvCompany.setAdapter(adapter);
        lvCompany.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent();
                intent.putExtra("expName", mapList.get(position).get("expName").toString());
                intent.putExtra("expCode", mapList.get(position).get("expCode").toString());
                setResult(RESULT_OK, intent);
                finish();
            }
        });
    }

    private void searchCompany() {
        showDialog(this);
        String url = Constant.URL_EXPRESS_COMPANY;
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("searchKey", company);
        JSONRequest.getInstance().sendRequest(JSONRequest.GET, url, map, handler, 0, HttpUtil.ACTION_URI);
    }

    Handler handler = new Handler() {
        public void handleMessage(android.os.Message msg) {
            closeDialog();
            switch (msg.what) {
                case 0:
                    /** 列表查询 */
                    mapList = JsonReponseHandler.getListFromJson(
                            String.valueOf(msg.obj), adapter, null);
                    break;
                default:
                    break;
            }
        }

        ;
    };

    @Override
    public void onClick(View v) {

        switch (v.getId()) {
            case R.id.tvCancel:
                finish();
                break;
            case R.id.iv_title_right:
                company = etSearch.getText().toString();
                if (StringUtil.isBlank(company)) {
                    ToastHelper.getInstance().displayToastWithQuickClose("请输入公司名称");
                    return;
                }
                searchCompany();
                break;
        }

    }

    private class MyAdapter extends FatherAdapter {

        @Override
        public BaseAdapter setList(List list) {

            return MyAdapter.this;
        }

        @Override
        public int getCount() {
            return mapList.size();
        }

        @Override
        public Object getItem(int position) {
            return mapList.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder;
            if (convertView == null) {
                holder = new ViewHolder();
                convertView = LayoutInflater.from(ExpressCompanyActivity.this).inflate(R.layout.item_textview, null);
                holder.tvCompany = (TextView) convertView.findViewById(R.id.tv1);
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }
            String company = String.valueOf(mapList.get(position).get("expName"));
            holder.tvCompany.setText(company);
            return convertView;
        }

    }

    class ViewHolder {
        TextView tvCompany;
    }

}
